class Shared::DepartmentsController < ApplicationController
  # GET /shared/departments
  # GET /shared/departments.xml
  def index
    @title_view = 'Departamentos'
    @shared_departments = Shared::Department.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @shared_departments }
       format.json  { render :json => @shared_departments }
    end
  end

  # GET /shared/departments/1
  # GET /shared/departments/1.xml
  def show
    @shared_department = Shared::Department.find(params[:id])
@title_view = 'Departamentos'
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @shared_department }
      format.json  { render :json => @shared_department }
    end
  end

  # GET /shared/departments/new
  # GET /shared/departments/new.xml
  def new
    if (user_signed_in? && ([2].include?(current_user.role)))
    @shared_department = Shared::Department.new
@title_view = 'Departamentos'
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @shared_department }
    end
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  # GET /shared/departments/1/edit
  def edit
    if (user_signed_in? && ([2].include?(current_user.role)))
    @shared_department = Shared::Department.find(params[:id])
    @title_view = 'Departamentos'
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  # POST /shared/departments
  # POST /shared/departments.xml
  def create
    if (user_signed_in? && ([2].include?(current_user.role)))
    @shared_department = Shared::Department.new(params[:shared_department])
@title_view = 'Departamentos'
    respond_to do |format|
      if @shared_department.save
        format.html { redirect_to(@shared_department, :notice => 'Se ha creado correctamente el departamento.') }
        format.xml  { render :xml => @shared_department, :status => :created, :location => @shared_department }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @shared_department.errors, :status => :unprocessable_entity }
      end
    end
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  # PUT /shared/departments/1
  # PUT /shared/departments/1.xml
  def update
    if (user_signed_in? && ([2].include?(current_user.role)))
    @shared_department = Shared::Department.find(params[:id])
@title_view = 'Departamentos'
    respond_to do |format|
      if @shared_department.update_attributes(params[:shared_department])
        format.html { redirect_to(@shared_department,  :notice => 'Se ha actualizado correctamente el departamento.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @shared_department.errors, :status => :unprocessable_entity }
      end
    end
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  # DELETE /shared/departments/1
  # DELETE /shared/departments/1.xml
  def destroy
    if (user_signed_in? && ([2].include?(current_user.role)))
      @shared_department = Shared::Department.find(params[:id])
      mensaje = @shared_department.erasable
      if mensaje.blank?
        @shared_department.deleted = 1
        @shared_department.save
      end
    respond_to do |format|
      format.html { redirect_to(shared_departments_url, :notice => mensaje) }
      format.xml  { head :ok }
    end
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end
end
